package com.luoshi.ucenterservice.controller;


import com.luoshi.common.exception.ucenter.MobileExistException;
import com.luoshi.common.exception.ucenter.MobileIsDisabledException;
import com.luoshi.common.exception.ucenter.MobileOrPasswordException;
import com.luoshi.common.exception.ucenter.PasswordException;
import com.luoshi.common.result.Result;
import com.luoshi.entity.base.ucenter.vo.LoginVo;
import com.luoshi.ucenterservice.service.UcenterMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 /**
 * @ClassName UcenterMemberController
 * @Description:会员控制器
 * @Author luochangqin
 * @Date 2021/5/23
 * @Version V1.0
 **/
@Api(tags = "会员控制器")
@RestController
@RequestMapping("/ucenterservice/ucenter")
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;

    @PostMapping("login")
    @ApiOperation(value = "手机用户登录")
    public Result loginUser(@ApiParam(name = "login",value = "手机号") @RequestBody LoginVo login)  {

      // 返回token值,令牌 使用jwt生成
        Map<String,Object> info = null;
          try {
               info =  memberService.login(login);
          }catch (Exception e){
              if (e instanceof MobileOrPasswordException){
                  MobileOrPasswordException e1 = (MobileOrPasswordException) e;
                  return  Result.error(e1.getCode(),e1.getMessage());
              }else if(e instanceof PasswordException){
                  PasswordException e2 = (PasswordException) e;
                  return Result.error(e2.getCode(),e.getMessage());
              }else if(e instanceof MobileIsDisabledException){
                  MobileIsDisabledException ex3= (MobileIsDisabledException) e;
                  return Result.error(ex3.getCode(),ex3.getMessage());
              }
          }
          return Result.success(info);
    }

    @PostMapping("register")
    @ApiOperation(value = "用户注册")
    public Result registerUser(@ApiParam(name ="registerVo",value = "注册前端数据封装") @RequestBody LoginVo loginVo) {
        Map<String, Object> register = null;
        try {
            register = memberService.register(loginVo);
        }catch (Exception e) {
            if (e instanceof MobileExistException) {
                MobileExistException mobileExistException = (MobileExistException) e;
                return Result.error(mobileExistException.getCode(),mobileExistException.getMessage());
            }
        }
        return Result.success(register);
    }
}

